﻿using App.Codes;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace App.Controllers
{
    public class MapController : Controller
    {
        public ActionResult Index(string Isback)
        {
            ViewData["Isback"] = Isback;
            return View();
        }
        public ActionResult OfficePerson(string id)
        {
            ViewData["officeid"] = id;
            return View();
        }

        public ActionResult FloorMap(string floor, string danweiguid)
        {
            ViewData["danweiguid"] = danweiguid;
            ViewData["floor"] = floor;
            return View();
        }


        public string GetMapTable()
        {

            StringBuilder dbTableHtml = new StringBuilder();
            dbTableHtml.Append("<tr>");

            dbTableHtml.Append("<td width='311' bgcolor='#ef6d21' class='text2'>分类</td>");
            dbTableHtml.Append("<td width='363' height='60' bgcolor='#ea500a' class='text'>楼层</td>");
            dbTableHtml.Append(" <td width='785' bgcolor='#ef6d21' class='text2'>楼层分布情况</td>");
            dbTableHtml.Append(" </tr>");

            //取得所有的Area
            DataTable dtArea =
                new ClsCon().CreatDaTable(
                    "SELECT Id,MyTexts FROM dbo.SysField WHERE MyTables='OfficeSpace' AND MyColums='Area' ORDER BY Sort");

            for (int i = 0; i < dtArea.Rows.Count; i++)
            {
                string areaText = Convert.ToString(dtArea.Rows[i]["MyTexts"]);
                string areaId = Convert.ToString(dtArea.Rows[i]["Id"]);


                //取得Area对应的Floor
                DataTable dtFloor =
                    new ClsCon().CreatDaTable(
                        "SELECT Id,MyTexts FROM dbo.SysField WHERE MyTables='OfficeSpace' AND MyColums='Floor' AND ParentId='" +
                        areaId + "' ORDER BY Sort");
                //遍历floor
                for (int j = 0; j < dtFloor.Rows.Count; j++)
                {
                    string floorText = Convert.ToString(dtFloor.Rows[j]["MyTexts"]);
                    //string floorId = Convert.ToString(dtFloor.Rows[i]["Id"]);

                    dbTableHtml.Append("<tr>");
                    if (j == 0)
                    {
                        dbTableHtml.Append("<td rowspan='" + (2 * dtFloor.Rows.Count - 1) +
                                           "' bgcolor='#f9d670' class='text3'>" + areaText + "</td>");
                    }

                    dbTableHtml.Append("<td height='65' class='text4'>" + floorText + "</td>");



                    dbTableHtml.Append("<td bgcolor='#f9d670'>");
                    //dbTableHtml.Append("<table width='100%' border='0' cellspacing='0' cellpadding='0'>");
                    ////插入某一层楼对应的
                    //DataTable dtOfficeSpace =
                    //    new ClsCon().CreatDaTable("SELECT No,Name,RowGuid FROM dbo.OfficeSpace WHERE Floor='" +
                    //                              floorText + "'");


                    //if (floorText == "2F" || floorText == "3F" || floorText == "4F" || floorText == "5F")
                    //{
                    //    dbTableHtml.Append("<tr>");

                    //    DataTable dtDanWeiGuid =
                    //        new ClsCon().CreatDaTable(
                    //            "SELECT DanWeiGuid FROM dbo.OfficeSpace WHERE Floor='" + floorText + "' GROUP BY DanWeiGuid");
                    //    //遍历danweiguid
                    //    for (int danweicount = 0; danweicount < dtDanWeiGuid.Rows.Count; danweicount++)
                    //    {
                    //        string danweiguid = Convert.ToString(dtDanWeiGuid.Rows[danweicount]["DanWeiGuid"]);
                    //        if (!string.IsNullOrEmpty(danweiguid))
                    //        {
                    //            string danweiname = new ClsCon().GetFieldValue("SELECT TOP 1 Name FROM dbo.SysDepartment WHERE Id='" + danweiguid + "'");

                    //            dbTableHtml.Append("<td class='text5' onclick=openmap('" +
                    //                           floorText + "','" +
                    //                           danweiguid + "') >" +
                    //                           danweiname + " </td>");
                    //        }
                    //    }


                    //    dbTableHtml.Append("</tr>");
                    //}
                    //else if (floorText == "6F" || floorText == "7F" || floorText == "8F")
                    //{
                    //    //编号
                    //    dbTableHtml.Append("<tr>");
                    //    for (int n = 0; n < dtOfficeSpace.Rows.Count; n++)
                    //    {

                    //        dbTableHtml.Append("<td class='text5' id='tdindex' onclick=openpersonlist('" +
                    //                           Convert.ToString(dtOfficeSpace.Rows[n]["RowGuid"]) + "') >" +
                    //                           Convert.ToString(dtOfficeSpace.Rows[n]["No"]) + " </td>");

                    //    }
                    //    dbTableHtml.Append("</tr>");

                    //    //名称
                    //    dbTableHtml.Append("<tr>");
                    //    for (int n = 0; n < dtOfficeSpace.Rows.Count; n++)
                    //    {


                    //        dbTableHtml.Append("<td class='text5' id='tdindex' onclick=openpersonlist('" +
                    //                       Convert.ToString(dtOfficeSpace.Rows[n]["RowGuid"]) + "') GetMapArea>" +
                    //                       dtOfficeSpace.Rows[n]["Name"] + " </td>");


                    //    }
                    //    dbTableHtml.Append("</tr>");
                    //}
                    
                    


                    //dbTableHtml.Append("</table>");


                    dbTableHtml.Append("<div style='width:100%;height:100%'>");
                    //插入某一层楼对应的
                    DataTable dtOfficeSpace =
                        new ClsCon().CreatDaTable("SELECT No,Name,RowGuid FROM dbo.OfficeSpace WHERE Floor='" +
                                                  floorText + "'");


                    if (floorText == "2F" || floorText == "3F" || floorText == "4F" || floorText == "5F")
                    {
                        dbTableHtml.Append("<ul>");

                        DataTable dtDanWeiGuid =
                            new ClsCon().CreatDaTable(
                                "SELECT DanWeiGuid FROM dbo.OfficeSpace WHERE Floor='" + floorText + "' GROUP BY DanWeiGuid");
                        //遍历danweiguid
                        for (int danweicount = 0; danweicount < dtDanWeiGuid.Rows.Count; danweicount++)
                        {
                            string danweiguid = Convert.ToString(dtDanWeiGuid.Rows[danweicount]["DanWeiGuid"]);
                            if (!string.IsNullOrEmpty(danweiguid))
                            {
                                string danweiname = new ClsCon().GetFieldValue("SELECT TOP 1 Name FROM dbo.SysDepartment WHERE Id='" + danweiguid + "'");

                                dbTableHtml.Append("<li class='text5' onclick=openmap('" +
                                               floorText + "','" +
                                               danweiguid + "') >" +
                                               danweiname + "&nbsp;</li>");
                            }
                        }


                        dbTableHtml.Append("</ul>");
                    }
                    else if (floorText == "6F" || floorText == "7F" || floorText == "8F")
                    {
                        //编号
                        //dbTableHtml.Append("<ul>");
                        //for (int n = 0; n < dtOfficeSpace.Rows.Count; n++)
                        //{

                        //    dbTableHtml.Append("<li class='text5' id='tdindex' onclick=openpersonlist('" +
                        //                       Convert.ToString(dtOfficeSpace.Rows[n]["RowGuid"]) + "') >" +
                        //                       Convert.ToString(dtOfficeSpace.Rows[n]["No"]) + " </li>");

                        //}
                        //dbTableHtml.Append("</ul>");

                        //名称
                        dbTableHtml.Append("<ul>");
                        for (int n = 0; n < dtOfficeSpace.Rows.Count; n++)
                        {
                            dbTableHtml.Append("<li class='text5' id='tdindex' onclick=openpersonlist('" +
                                           Convert.ToString(dtOfficeSpace.Rows[n]["RowGuid"]) + "') GetMapArea>" +
                                           dtOfficeSpace.Rows[n]["Name"] + "&nbsp;</li>");


                        }
                        dbTableHtml.Append("</ul>");
                    }




                    dbTableHtml.Append("</div>");
                    dbTableHtml.Append("</td>");

                    dbTableHtml.Append("</tr>");


                    if (j < dtFloor.Rows.Count - 1)
                    {
                        //加入边框
                        dbTableHtml.Append("<tr>");
                        dbTableHtml.Append("<td height='1' bgcolor='#e94f09'></td>");
                        dbTableHtml.Append("<td bgcolor='#e94f09'></td>");
                        dbTableHtml.Append("</tr>");
                    }
                    else
                    {
                        if (i < dtArea.Rows.Count - 1)
                        {
                            dbTableHtml.Append("<tr>");
                            dbTableHtml.Append("<td bgcolor='#e94f09'></td>");
                            dbTableHtml.Append("<td height='2' bgcolor='#e94f09'></td>");
                            dbTableHtml.Append("<td bgcolor='#e94f09'></td>");
                            dbTableHtml.Append("</tr>");
                        }
                    }
                }
            }
            return Convert.ToString(dbTableHtml);
        }

        public string GetOfficePerson(string officeid)
        {
            StringBuilder sbTableHtml = new StringBuilder();
            DataTable dtPerson = new ClsCon().CreatDaTable("SELECT Id,MyName,TXIMG,Position,PhoneNumber FROM dbo.SysPerson WHERE OfficeSpaceGuid='" + officeid + "'");
            sbTableHtml.Append("<tr><td width='311' class='text2'>姓名</td><td width='363' height='60' class='text'>基本信息</td><td width='785' class='text2'>分管业务</td></tr>");
            for (int i = 0; i < dtPerson.Rows.Count; i++)
            {
                string personid = Convert.ToString(dtPerson.Rows[i]["Id"]);
                string personname = Convert.ToString(dtPerson.Rows[i]["MyName"]);
                string personimg = Convert.ToString(dtPerson.Rows[i]["TXIMG"]);
                string personPosition = Convert.ToString(dtPerson.Rows[i]["Position"]);
                string personPhoneNumber = Convert.ToString(dtPerson.Rows[i]["PhoneNumber"]);

                sbTableHtml.Append("<tr>");

                sbTableHtml.Append("<td>");
                sbTableHtml.Append("<table><tr><td class='tdimg'>");
                sbTableHtml.Append("<img src='../../up/" + personimg + "' class='pictureclass' />");
                sbTableHtml.Append("</td><td class='tdname'>");
                sbTableHtml.Append(personname);

                sbTableHtml.Append(" </td></tr></table>");

                sbTableHtml.Append("</td><td><table class='tdtable'><tr><td class='tdname'>职务： ");
                sbTableHtml.Append(personPosition);
                sbTableHtml.Append("</td></tr><tr><td class='tdname'>联系方式：");
                sbTableHtml.Append(personPhoneNumber);


                sbTableHtml.Append("</td></tr></table> ");
                sbTableHtml.Append("</td>");

                sbTableHtml.Append("<td>");
                sbTableHtml.Append("<table class='tdtable'>");
                DataTable dtBusiness = new ClsCon().CreatDaTable("SELECT BusinessName,CaiLiao FROM dbo.View_PersonAndBusiness WHERE Id='" + personid + "'");
                for (int j = 0; j < dtBusiness.Rows.Count; j++)
                {
                    sbTableHtml.Append("<tr>");
                    sbTableHtml.Append("<td class='redtdname'>");
                    sbTableHtml.Append(Convert.ToString(dtBusiness.Rows[j]["BusinessName"]));
                    sbTableHtml.Append("</td>");
                    sbTableHtml.Append("</tr>");

                    sbTableHtml.Append("<tr>");
                    sbTableHtml.Append("<td class='tdname'>");
                    sbTableHtml.Append("办理该业务您需要带"+Convert.ToString(dtBusiness.Rows[j]["CaiLiao"])+"。谢谢您的配合");
                    sbTableHtml.Append("</td>");
                    sbTableHtml.Append("</tr>");

                }
                sbTableHtml.Append("</table>");
                sbTableHtml.Append("</td>");

                sbTableHtml.Append("</tr>");
            }

            
            if (dtPerson.Rows.Count >= 3)
            {
                sbTableHtml.Append("<tr style='display:none'>");
                sbTableHtml.Append("<td>");
                sbTableHtml.Append("<input id='ismarquee' value='"+"1"+"' />");
                sbTableHtml.Append("</td>");
                sbTableHtml.Append("</tr>");
            }
            return Convert.ToString(sbTableHtml);
        }

        public string GetOfficePersonSimple(string officeid)
        {
            StringBuilder sbTableHtml = new StringBuilder();
            DataTable dtPerson = new ClsCon().CreatDaTable("SELECT Id,MyName,TXIMG,Position,PhoneNumber FROM dbo.SysPerson WHERE OfficeSpaceGuid='" + officeid + "'");
            sbTableHtml.Append("<tr><td>姓名</td><td  height='60'>基本信息</td><td>分管业务</td></tr>");
            if (dtPerson.Rows.Count<=0)
            {
                return "NoPerson";
            }
            for (int i = 0; i < dtPerson.Rows.Count; i++)
            {
                string personid = Convert.ToString(dtPerson.Rows[i]["Id"]);
                string personname = Convert.ToString(dtPerson.Rows[i]["MyName"]);
                string personimg = Convert.ToString(dtPerson.Rows[i]["TXIMG"]);
                string personPosition = Convert.ToString(dtPerson.Rows[i]["Position"]);
                string personPhoneNumber = Convert.ToString(dtPerson.Rows[i]["PhoneNumber"]);

                sbTableHtml.Append("<tr>");

                sbTableHtml.Append("<td>");
                sbTableHtml.Append("<table><tr><td>");
                
                sbTableHtml.Append("</td><td>");
                sbTableHtml.Append(personname);

                sbTableHtml.Append(" </td></tr></table>");

                sbTableHtml.Append("</td><td><table><tr><td>职务： ");
                sbTableHtml.Append(personPosition);
                sbTableHtml.Append("</td></tr><tr><td>联系方式：");
                sbTableHtml.Append(personPhoneNumber);

                sbTableHtml.Append("</td></tr></table> ");
                sbTableHtml.Append("</td>");

                sbTableHtml.Append("<td>");
                sbTableHtml.Append("<table>");


                DataTable dtBusiness = new ClsCon().CreatDaTable("SELECT BusinessName,CaiLiao FROM dbo.View_PersonAndBusiness WHERE Id='" + personid + "'");
                for (int j = 0; j < dtBusiness.Rows.Count; j++)
                {
                    sbTableHtml.Append("<tr>");
                    sbTableHtml.Append("<td>");
                    sbTableHtml.Append(Convert.ToString(dtBusiness.Rows[j]["BusinessName"]));
                    sbTableHtml.Append("</td>");
                    sbTableHtml.Append("</tr>");

                    //sbTableHtml.Append("<tr>");
                    //sbTableHtml.Append("<td>");
                    //sbTableHtml.Append("所需材料：" + Convert.ToString(dtBusiness.Rows[j]["CaiLiao"]));
                    //sbTableHtml.Append("</td>");
                    //sbTableHtml.Append("</tr>");

                }
                sbTableHtml.Append("</table>");
                sbTableHtml.Append("</td>");
                sbTableHtml.Append("</tr>");
            }
            return Convert.ToString(sbTableHtml);
        }

        public string GetMapArea(string floor,string danweiguid)
        {
            DataTable dtMapArea =
                new ClsCon().CreatDaTable("SELECT Area,RowGuid FROM dbo.OfficeSpace WHERE Floor='" + floor + "' AND DanWeiGuid='" + danweiguid + "'");



            return JsonConvert.SerializeObject(dtMapArea);
        }


         
    }
}
